package com.fish.yssb.ui.page.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fish.yssb.R;
import com.fish.yssb.api.setApi;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.data.model.LoginUser;
import com.fish.yssb.db.RealmDb;
import com.fish.yssb.db.bean.LogInfoTable;
import com.fish.yssb.db.bean.UserInfoTable;
import com.fish.yssb.ui.page.activity.his.hisDetailYCActivity;
import com.fish.yssb.ui.page.activity.other.AddUserActivity;
import com.fish.yssb.ui.page.activity.other.UserManageActivity;
import com.fish.yssb.ui.page.dialog.PublicNoticeDialog;
import com.fish.yssb.ui.page.dialog.SetParaDialog;
import com.fish.yssb.ui.page.dialog.getDataByTimeDialog;

import java.util.ArrayList;


/**
 * Created by WangChang on 2016/4/1.
 */
public class UserManageParaAdapter extends RecyclerView.Adapter<UserManageParaAdapter.BaseViewHolder> {
    public ArrayList<UserInfoTable> dataList = new ArrayList<UserInfoTable>();
    private Resources res;
    private Context m_context;
    public UserManageActivity activity;
    public void replaceAll(ArrayList<UserInfoTable> list) {
        dataList.clear();
        if (list != null && list.size() > 0) {
            dataList.addAll(list);
        }
        notifyDataSetChanged();
    }
//    private getDataByTimeDialog noticeDialog;

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        m_context =parent.getContext();
        return new realtimeYCViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.user_manage_item, parent, false));
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {

        holder.setData(dataList.get(position),position);

    }

    private String strVal ="";

    @Override
    public int getItemCount() {
        return dataList != null ? dataList.size() : 0;
    }

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        void setData(UserInfoTable data,int position) {
        }
    }

    private class realtimeYCViewHolder extends BaseViewHolder {

        private PublicNoticeDialog noticeDialog;
//        private SetParaDialog setParaDialog;
        private TextView tv_name;
        private TextView tv_addr;
        private TextView tv_val;
        private TextView tv_op;
        private LinearLayout ll_view;
        public realtimeYCViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
            tv_addr = view.findViewById(R.id.tv_addr);
            tv_val = view.findViewById(R.id.tv_val);
            tv_op = view.findViewById(R.id.tv_op);
            ll_view = view.findViewById(R.id.ll_view);
        }



        @Override
        void setData(final UserInfoTable data,int position) {
            if (data != null) {
                tv_name.setText(position+1+"");
                tv_addr.setText(data.getName());
                tv_val.setText(data.getType());

                tv_op.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        final LoginUser user = BaseApp.getInstance().user;
                        if(!"管理员".equalsIgnoreCase(user.getUserType())) {
                            noticeDialog = new PublicNoticeDialog((Activity) m_context, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    String sendStatus = "";
                                    if (view.getId() == R.id.btn_chancel_notice) {//暂存

                                        noticeDialog.dismiss();
                                    } else {//生成

                                        noticeDialog.dismiss();

                                    }
                                    noticeDialog.dismiss();

                                }
                            }, 4, "", "非管理员用户无权限查进行用户管理操作", "取消", "确定");
                            noticeDialog.show();

                        }else {

                            noticeDialog = new PublicNoticeDialog((Activity) m_context, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    String sendStatus = "";
                                    if (view.getId() == R.id.btn_chancel_notice) {//暂存

                                        noticeDialog.dismiss();
                                    } else {//生成
                                        RealmDb.getInstance().deleteAllRealmUserInfoTable(data);
                                        activity.refeshdata();
                                        noticeDialog.dismiss();

                                    }
                                    noticeDialog.dismiss();

                                }
                            }, 1, "", "确认删除该用户？", "取消", "确认");
                            noticeDialog.show();
                        }




                    }
                });
            }


        }
    }


}
